<?php

namespace App\Models\Posts;

use App\Models\WxCircle;
use App\Models\WxUser;
use App\Models\WxUserFollow;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class WxPost extends Model
{
    use HasDateTimeFormatter;

//    use SoftDeletes;

    protected $table = 'wx_posts';

    protected $appends = [];

    protected $fillable = ['posts_content', 'user_id', 'circle_id', 'circle_name', 'address_id', 'tags_id'];

    /**  被重载的数据保存在此  */
    private $data = array();

    function __construct(array $attributes = [])
    {

        parent::__construct($attributes);
    }


    public function circle()
    {
        return $this->belongsTo(WxCircle::class, 'circle_id', 'id');
    }

    public function comment()
    {
        return $this->hasMany(WxComment::class, 'posts_id', 'id');
    }

    public function user()
    {
        return $this->hasOne(WxUser::class, 'id', 'user_id');
    }

    public function images()
    {
        return $this->hasMany(WxPostsImg::class, 'posts_id', 'id');
    }

    public function address()
    {
        return $this->hasOne(WxPostsAddress::class, 'id', 'address_id');
    }


    public function video()
    {
        return $this->hasOne(WxPostsVideo::class, 'posts_id', 'id');
    }

    // 远程关联
    public function tags()
    {
        return $this->hasManyThrough(
            WxTag::class,
            WxPostsTag::class,
            'posts_id',
            'id',
            'id',
            'id'
        );
    }

    public function like()
    {
        return $this->hasMany(WxLike::class, 'posts_id', 'id');
    }


    public function userFollow()
    {
        return $this->hasMany(WxUserFollow::class, 'user_follow_id', 'user_id');
    }

    /**
     * 文章内容获取器
     *
     * @param string $value
     * @return string
     */
    public function getPostsContentAttribute($value)
    {
        return str_replace("\n", "<br>", str_replace(" ", " ", $value));;
    }


    /**
     * 判断是否是我自己的帖子
     *
     * @return string
     */
    public static function isMyPosts($uid, $posts_id)
    {
        $postsUserId = WxPost::where('id', $posts_id)->pluck('user_id')->first();

        if ($uid == $postsUserId) {
            return true;
        } else {
            return false;
        }

    }


    /**
     * 获取器判断是否关注该用户
     *
     * @return string
     */
    public static function isFollowUser($uid, $user_follow_id)
    {
        $uid = $uid; // 访问的用户id
        $exists = WxUserFollow::where('user_id', $uid)->where('user_follow_id', $user_follow_id)->exists();
        return $exists;

    }

    /**
     * 获取器判断用户是否喜欢
     *
     * @return string
     */
    public static function isLike($uid, $posts_id)
    {

        $exists = WxLike::where('user_id', $uid)->where('posts_id', $posts_id)->exists();
        return $exists;

    }


    /**
     * 获取器判断是否收藏
     *
     * @return string
     */
    public static function isCollec($uid, $posts_id)
    {

        $exists = WxCollect::where('user_id', $uid)->where('posts_id', $posts_id)->exists();
        return $exists;

    }

    public function getFormatTimeAttribute()
    {
        return format_datetime($this->created_at);

    }

    // 判断文章是否超出
    public function getIsContentBeyondAttribute()
    {
        $textCount = mb_strlen($this->posts_content);
        $isBr = substr_count($this->posts_content, '<br>');
        if ($isBr >= 3 || $textCount >= 70) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取一张图片
     * @return mixed
     */
    public function getImageaAttribute()
    {
        $data = [];

        $postsVideo = WxPostsVideo::where('posts_id', $this->id)->first(['video_thumb_url']);
        $data['img_url'] = $postsVideo['video_thumb_url'];
        if (!$data['img_url']) {
            $postsImg = WxPostsImg::where('posts_id', $this->id)->first(['img_url']);
            $data['img_url'] = $postsImg['img_url'];
        }

        return $data;
    }

    /**
     * 前端用
     */
    public function getIsEllipsisAttribute()
    {
        return false;
    }


    /**
     * 获取图片组
     */
    public function getImagesUrlAttribute()
    {
        return WxPostsImg::where('posts_id', $this->id)->get()->map(function ($v) {
            return $v['img_url'];
        });
    }


    /**
     * 获取评论
     */
    public function getComment($posts_id, $field = ["*"])
    {
        return WxComment::where('comment_state', 1)->where('posts_id', $posts_id)->limit(2)->orderBy('id', 'desc')->get($field);
    }

    /**
     * 获取评论数量
     */
    public function getCommentCount($posts_id)
    {

        return WxComment::where('comment_state', 1)->where('posts_id', $posts_id)->count();
    }



}
